package com.hmyuan.controller.front;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.hmyuan.domain.Article;
import com.hmyuan.domain.Article_category;
import com.hmyuan.domain.Category;
import com.hmyuan.domain.Sys_user;
import com.hmyuan.domain.Tag;
import com.hmyuan.service.ArticleService;
import com.hmyuan.service.CategoryService;
import com.hmyuan.service.CommentService;
import com.hmyuan.service.TagService;
import com.hmyuan.util.PageUtil;
import com.hmyuan.util.UserUtil;

/**
 * 文章controller
 * @author hmyuan
 *
 */
@Controller
@RequestMapping("/article")
public class FrontArticleController {

	@Resource
	ArticleService articleService;
	@Resource
	CategoryService categoryService;
	@Resource
	TagService tagService;
	@Resource
	CommentService commentService;
	@Resource
	PageUtil pageUtil;
	/**
	 * 分页查询文章列表
	 */
	@ResponseBody
	@RequestMapping("/findarticleList")
	public PageInfo<Article> findarticleList(HttpServletRequest request,Model model,Integer pageNum,Article article){
		if(pageNum==null) pageNum=1;
		article.setNotstatus(2);
		return articleService.selectListWithCollectionByPage(article, pageNum, 10);
	}
	/**
	 * 分页查询文章列表根据标签检
	 */
	@ResponseBody
	@RequestMapping("/findarticleListbytag")
	public PageInfo<Article> findarticleListbytag(HttpServletRequest request,Model model,Integer pageNum,Long tagid){
		if(pageNum==null) pageNum=1;
		Article article = new Article();
		article.setSearchtag(tagid);
		article.setNotstatus(2);
		return articleService.selectListWithCollectionByPage(article, pageNum, 10);
	}
	/**
	 * 分页查询文章列表根据类别
	 */
	@ResponseBody
	@RequestMapping("/findarticleListbycategory")
	public PageInfo<Article> findarticleListbycategory(HttpServletRequest request,Model model,Integer pageNum,Long categoryid){
		if(pageNum==null) pageNum=1;
		Article article = new Article();
		article.setSearchcategory(categoryid);
		article.setNotstatus(2);
		return articleService.selectListWithCollectionByPage(article, pageNum, 10);
	}
	/**
	 * 编辑文章信息页面
	 */
	@RequestMapping("/content/{id}")
	public String articleUpdatePage(HttpServletRequest request,Model model,@PathVariable Long id){
		if(null==id) return "/article/articleAddPage";
		Article article = articleService.selectByWithCollectionPrimaryKey(id);
		article.setContent(article.getContent().replaceAll("~~fileDomainName~~",(String)pageUtil.getValueOfAplication("fileDomainName")));
		request.setAttribute("article", article);
		request.setAttribute("commentList", commentService.selectListByArticleid(id));
		
		/*浏览量加一*/
		Article articleup = new Article();
		articleup.setClicknum(article.getClicknum()+1);
		articleup.setId(article.getId());
		articleService.updateByPrimaryKeySelective(articleup);
		
		request.setAttribute("titleset", "文章详情");
		
		//设置页面描述
		request.setAttribute("siteTitle", article.getTitle());
		request.setAttribute("siteDescript", article.getSummary());
		String tagName = Tag.getNameStringByList(article.getTagList(), ",");
		String categoryName = Category.getNameStringByList(article.getCategoryList(), ","); 
		request.setAttribute("siteKeyword", categoryName+(categoryName.length()==0||tagName.length()==0?"":",")+tagName);
		return "/front/article/content";
	}
	
	
}
